'''
1、计算网络的节点数、边数、平均度等并返回
2、统计某个节点的属性的分布
'''
import networkx as nx
import numpy as np
import matplotlib as plt
def get_node_number(G):
    '''计算节点数'''
    number_nodes=len(list(G.nodes()))
    return number_nodes
def get_edge_number(G):
    '''计算边数'''
    number_edges=len(list(G.edges))
    return number_edges
def cal_average_degree(G):
    '''计算平均度'''
    average=2*get_edge_number(G)/get_node_number(G)
    return average
def cal_dgree_distribution(G):
    '''计算度的频率分布'''
    dict_degree={}
    for node in G.nodes():
        de=len(list(G.neighbors(node))) #每个点的度
        if str(de) in dict_degree.keys():
            dict_degree[str(de)]=dict_degree[str(de)]+1
        else:
            dict_degree[str(de)]=1 #构建不同度频度的字典
    for i in dict_degree.keys():
        dict_degree[i]=dict_degree[i]/len(list(G.nodes()))#度的频率
    return dict_degree
def cal_attribution_distribution(G,attribution):
    '''计算某属性的频率分布'''
    dic={}
    li=[]
    for i in list(G.nodes):#该属性各个值出现的次数
        if G.nodes[i][attribution] not in dic.keys():
            dic[G.nodes[i][attribution]]=1
        else:
            dic[G.nodes[i][attribution]]=dic[G.nodes[i][attribution]]+1
    for i in dic.keys():
        dic[i]=dic[i]/len(list(G.nodes()))#该属性不同值出现的频率
    return dic

